// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.process.parts;

import java.beans.PropertyChangeEvent;
import java.util.List;

import org.dengues.commons.IDenguesCoreContants;
import org.dengues.core.ExceptionOperation;
import org.dengues.core.external.AbstractCompNodeExt;
import org.dengues.core.process.CompElement;
import org.dengues.designer.ui.editors.ProcessMultiEditor;
import org.dengues.designer.ui.process.commands.ExternalNodeChangeDataCommand;
import org.dengues.designer.ui.process.figures.CompBlockFigure;
import org.dengues.designer.ui.process.models.CompNode;
import org.dengues.designer.ui.process.policies.CompNodeEditPolicy;
import org.dengues.designer.ui.process.policies.CompNodeGraphicalEditPolicy;
import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.NodeEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-4-12 qiang.zhang $
 * 
 */
public class CompNodePart extends AbstractProcessPart implements NodeEditPart {

    private CompBlockFigure figure;

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.process.parts.AbstractProcessPart#getCastModel()
     */
    @Override
    protected CompElement getCastModel() {
        return (CompElement) getModel();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
     */
    @Override
    protected IFigure createFigure() {
        figure = new CompBlockFigure(getCastModel());
        // figure = new CompNodeFigure(getCastModel());
        // figure.setStart(getCastModel().getIncomingConnections().size() <= 0);
        figure.setHint(((CompNode) getCastModel()).getComponent().getLongName());
        return figure;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
     */
    @Override
    protected void createEditPolicies() {
        installEditPolicy(EditPolicy.COMPONENT_ROLE, new CompNodeEditPolicy());
        installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new CompNodeGraphicalEditPolicy());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#refreshVisuals()
     */
    @Override
    protected void refreshVisuals() {
        Point loc = getCastModel().getLocation();
        Dimension size = getCastModel().getSize();
        Rectangle rectangle = new Rectangle(loc, size);
        ((GraphicalEditPart) getParent()).setLayoutConstraint(this, getFigure(), rectangle);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
     */
    public void propertyChange(PropertyChangeEvent e) {
        String prop = e.getPropertyName();
        if (CompNode.PROP_NODE_LOCATION.equals(prop) || CompNode.PROP_NODE_SIZE.equals(prop)) {
            refreshVisuals();
        } else if (CompNode.PROP_OUTGOINGS.equals(prop)) {
            refreshTargetConnections();
        } else if (CompNode.PROP_INCOMINGS.equals(prop)) {
            refreshSourceConnections();
            // figure.setStart(getCastModel().getIncomingConnections().size() <= 0);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getModelSourceConnections()
     */
    @Override
    protected List getModelSourceConnections() {
        return getCastModel().getIncomingConnections();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getModelTargetConnections()
     */
    @Override
    protected List getModelTargetConnections() {
        return getCastModel().getOutgoingConnections();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.ConnectionEditPart)
     */
    public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart connection) {
        return new ChopboxAnchor(getFigure());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.Request)
     */
    public ConnectionAnchor getSourceConnectionAnchor(Request request) {
        return new ChopboxAnchor(getFigure());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef.ConnectionEditPart)
     */
    public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart connection) {
        return new ChopboxAnchor(getFigure());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef.Request)
     */
    public ConnectionAnchor getTargetConnectionAnchor(Request request) {
        return new ChopboxAnchor(getFigure());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#performRequest(org.eclipse.gef.Request)
     */
    @Override
    public void performRequest(Request req) {
        IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        IWorkbenchPart part = activePage.getActiveEditor();
        if (part instanceof ProcessMultiEditor) {
            if (REQ_OPEN.equals(req.getType())) {
                if (getCastModel() instanceof CompNode) {
                    CompNode compNode = (CompNode) getCastModel();
                    if (compNode.getComponent().isExternalNode()) {
                        AbstractCompNodeExt externalNode = compNode.getExternalNode(true);
                        if (externalNode != null) {
                            int openShell = externalNode.openShell();
                            if (openShell == SWT.OK) {
                                CommandStack commandStack = ((ProcessMultiEditor) part).getDesignerEditor().getCommandStack();
                                ExternalNodeChangeDataCommand command = new ExternalNodeChangeDataCommand(compNode, externalNode);
                                commandStack.execute(command);
                            }
                        }
                    } else {
                        try {
                            activePage.showView(IDenguesCoreContants.VIEW_COMPONENT_PROPERTY_ID);
                        } catch (PartInitException e) {
                            ExceptionOperation.operate(e);
                        }
                    }
                }
            }
        }
        super.performRequest(req);
    }
}
